/*
 * DateSorter.java
 *
 * Created on June 22, 2007, 8:20 PM
 *
 *  APPLICATION NAME
    Copyright (C) 2007  Anand Kishore (anand@semanticvoid.com)
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package com.semanticvoid.lifelogger.karma.search.factory.decorator;

import com.semanticvoid.lifelogger.configuration.Constants;
import com.semanticvoid.lifelogger.entity.BrowseHistoryEntity;
import com.semanticvoid.lifelogger.entity.SearchHistoryEntity;
import com.semanticvoid.lifelogger.entity.comparator.AscendingTimeComparator;
import com.semanticvoid.lifelogger.entity.comparator.DescendingTimeComparator;
import com.semanticvoid.lifelogger.karma.search.interfaces.ISearch;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Vector;

/**
 *
 * @author Andy
 */
public class DateSorter implements ISearch {
    
    /** Searcher object */
    ISearch searcher;
    
    /** Sort order */
    short sortOrder;
    
    /** Creates a new instance of DateSorter */
    public DateSorter(ISearch searcher, short sortOrder) {
        this.searcher = searcher;
        
        this.sortOrder = sortOrder;
    }
    
    public Vector<BrowseHistoryEntity> searchBrowseHistory(String query, short type, String fromDate, String toDate) {
        Vector<BrowseHistoryEntity> results = searcher.searchBrowseHistory(query, type, fromDate, toDate);
        
        // Sort
        Collections.sort((List)results, getComparator(sortOrder));
        
        return results;
    }
    
    public Vector<BrowseHistoryEntity> searchBrowseHistory(String query, short browseType) {
        return searchBrowseHistory(query, browseType, null, null);
    }
    
    public Vector<SearchHistoryEntity> searchSearchHistory(String query, String fromDate, String toDate) {
        Vector<SearchHistoryEntity> results = searcher.searchSearchHistory(query, fromDate, toDate);
        
        // Sort
        Collections.sort((List)results, getComparator(sortOrder));
        
        return results;
    }
    
    public Vector<SearchHistoryEntity> searchSearchHistory(String query) {
        return searchSearchHistory(query, null, null);
    }

    private Comparator getComparator(short type) {
        if(type == Constants.SORT_ORDER_DESC) {
            return new DescendingTimeComparator();
        } else if(type == Constants.SORT_ORDER_ASC) {
            return new AscendingTimeComparator();
        }
        
        // default
        return new DescendingTimeComparator();
    }
}
